<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="学院课程：">
            <el-input
              v-model="listQuery.courseName"
              class="input-width"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="社区名称：">
            <el-input
              v-model="listQuery.communityName"
              class="input-width"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input
              v-model="listQuery.phone"
              class="input-width"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="身份信息：">
            <el-select
              v-model="listQuery.userIdentity"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in userIdentityOptions"
                :label="item.label"
                :key="item.value"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="签到状态：">
            <el-select
              v-model="listQuery.signStatus"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option label="全部" value=""></el-option>
              <el-option label="已签到" value="01"></el-option>
              <el-option label="已签退" value="02"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="exportFile()">
        导出
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column label="专题" align="center">
          <template slot-scope="scope">{{ scope.row.trainingName }}</template>
        </el-table-column>
        <el-table-column label="课程" align="center">
          <template slot-scope="scope">{{ scope.row.courseName }}</template>
        </el-table-column>
        <el-table-column label="社区" align="center">
          <template slot-scope="scope">{{ scope.row.communityName }}</template>
        </el-table-column>
        <el-table-column label="身份信息" align="center">
          <template slot-scope="scope">{{
            userIdentityList[scope.row.userIdentity]
          }}</template>
        </el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="手机号" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="签到时间" align="center">
          <template slot-scope="scope">{{
            scope.row.signInTime | formatTime
          }}</template>
        </el-table-column>
        <el-table-column label="签退时间" align="center">
          <template slot-scope="scope">{{
            scope.row.signOutTime | formatTime
          }}</template>
        </el-table-column>
        <el-table-column label="完成观看时间" align="center">
          <template slot-scope="scope">{{
            scope.row.updateTime | formatTime
          }}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  TiTrainingSignControllerList,
  exportSignResultList
} from '@/api/college'
import moment from 'moment'

import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'List',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      loading: false,
      userIdentityOptions: [
        {
          value: '01',
          label: '居民'
        },
        {
          value: '02',
          label: '居委会'
        },
        {
          value: '03',
          label: '物业公司'
        },
        {
          value: '04',
          label: '业委会'
        },
        {
          value: '05',
          label: '物管会'
        },
        {
          value: '06',
          label: '管理员'
        },
        {
          value: '07',
          label: '供应商'
        },
        {
          value: '08',
          label: '政府组织'
        },
        {
          value: '09',
          label: '其他'
        }
      ],
      userIdentityList: { '01': '居民', '02': '居委会', '03': '物业公司', '04': '业委会', '05': '物管会', '06': '管理员', '07': '供应商', '08': '政府组织', '09': '其他' }
    }
  },
  created () {
    this.getList()
  },
  filters: {
    formatTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    getList () {
      this.listLoading = true
      const params = {
        ...this.listQuery
      }
      TiTrainingSignControllerList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    exportFile () {
      this.listLoading = true
      const downloadName = '签到记录导出'
      const params = {
        ...this.listQuery
      }
      exportSignResultList(params).then(res => {
        this.listLoading = false
        if (res) {
          const blob = new Blob([res], { type: res.type })
          const A = document.createElement('a')
          const href = window.URL.createObjectURL(blob)
          A.href = href
          const addXlsx = navigator.userAgent.indexOf('Firefox') > -1 ? '.xls' : ''
          A.download = `${downloadName || '导出记录'}-${moment(new Date()).format('YYYYMMDDHHmmssSSS')}${addXlsx}.xls`
          document.body.appendChild(A)
          A.click()
          document.body.removeChild(A)
          window.URL.revokeObjectURL(href)
          this.$message.success(`${A.download} 开始下载`)
        } else {
          this.$message.error('导出失败')
        }
      })
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
